package tcu.iot.gasmanage.common.utils;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.impl.DefaultJwtParserBuilder;
import io.jsonwebtoken.security.Keys;
import tcu.iot.gasmanage.entity.Token;
import tcu.iot.gasmanage.entity.user.User;
import tcu.iot.gasmanage.mapper.TokenMapper;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.*;

import static io.jsonwebtoken.SignatureAlgorithm.HS256;

public class JwtUtils {
    /**
     * 过期时间---24 hour
     */
    private static final int EXPIRATION_TIME = 60 * 60 * 24;
    /**
     * 自己设定的秘钥
     */
    private static final String SECRET = "023bdc63c3c5a4587*9ee6581508b9d03ad39a74fc0c9a9cce604743367c9646b";
    /**
     * 前缀
     */
    public static final String TOKEN_PREFIX = "Bearer ";
    /**
     * 表头授权
     */
    public static final String AUTHORIZATION = "Authorization";

    public static Token generateToken(User user) {
        Calendar calendar = Calendar.getInstance();
        Date now = calendar.getTime();
        // 设置签发时间
        calendar.setTime(new Date());
        // 设置过期时间
        // 添加秒钟
        calendar.add(Calendar.SECOND, EXPIRATION_TIME);
        Date time = calendar.getTime();
        HashMap<String, Object> map = new HashMap<>();
        //you can put any data in the map
        map.put("id", user.getId());
        map.put("username", user.getUsername());
        map.put("password", user.getPassword());
        map.put("name", user.getName());
        map.put("roles", user.getRoles());
        map.put("avatar", user.getAvatar());
        map.put("introduction", user.getIntroduction());
        String jwt = Jwts.builder()
                .setClaims(map)
                //签发时间
                .setIssuedAt(now)
                //过期时间
                .setExpiration(time)
                .signWith(Keys.hmacShaKeyFor(SECRET.getBytes(StandardCharsets.UTF_8)), HS256)
                .compact();
        //jwt前面一般都会加Bearer

        return new Token(UUID.randomUUID().toString(), user.getUsername(), TOKEN_PREFIX + jwt, now, time);
    }

    public static HashMap<String, Object> validateToken(String token) {
        // parse the token.
        Map<String, Object> body = new DefaultJwtParserBuilder()
                .setSigningKey(Keys.hmacShaKeyFor(SECRET.getBytes(StandardCharsets.UTF_8)))
                .build()
                .parseClaimsJws(token.replace(TOKEN_PREFIX, ""))
                .getBody();  // JWT(过期）

        String id = body.get("id").toString();
        String username = body.get("username").toString();
        String roles = body.get("roles").toString();
        String avatar = body.get("avatar").toString();
        String introduction = body.get("introduction").toString();
        String password = body.get("password").toString();
        String name = body.get("name").toString();

        List<String> list = new ArrayList<>();
        list.add(roles);

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("roles", list);
        hashMap.put("id", id);
        hashMap.put("username", username);
        hashMap.put("avatar", avatar);
        hashMap.put("introduction", introduction);
        hashMap.put("password", password);
        hashMap.put("name", name);

        return hashMap;
    }
}
